package com.smt.modules.app.controller;

import com.smt.common.enums.Code;
import com.smt.common.utils.AliyunOSSUtil;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.R;
import com.smt.modules.app.annotation.FileSize;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.app.annotation.LoginUser;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.pojo.ProjectSiPojo;
import com.smt.modules.bus.service.LocalRemoteService;
import com.smt.modules.bus.vo.request.ProjectSiReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 文件操作
 *
 * @author zhangyi
 * @date 2018/10/10 16:40
 */
@RestController
@RequestMapping("/app/file")
@Api(tags = {"AppUploadController"},description = "文件操作(客户端接口)")
public class AppUploadController {

    @Resource
    private AliyunOSSUtil aliyunOSSUtil;

    @Resource
    private LocalRemoteService localRemoteService;

    /**
     * 上传项目文件
     *
     * @param projectSiReq
     * @param file
     * @param red
     * @param entity
     * @return
     * @throws IOException
     */
    @Login
    @PostMapping("/upload")
    @ApiOperation(value = "上传项目文件",httpMethod = "POST")
    public R file(ProjectSiReq projectSiReq,
                  @RequestParam("file") MultipartFile file,
                  RedirectAttributes red,
                  @LoginUser MemberUserEntity entity) throws IOException {
        Long userId = entity.getUserId();
        if(file.isEmpty()){
            red.addAttribute("failMsg","文件选择不能为空");
            return R.error(EnumToKeyUtil.toInt(Code.REQUEST_PARAM_ERROR),"无效文件");
        }
        localRemoteService.localSaveFile(file,userId,projectSiReq);
//        异步存储文件信息
        localRemoteService.remoteSaveFile(file,userId,projectSiReq);
        String fileUrl = localRemoteService.getFileUrl(projectSiReq);
        return R.ok().put("filePath",fileUrl);
    }

    /**
     * 上传非项目文件
     *
     * @param file
     * @param red
     * @param entity
     * @return
     * @throws IOException
     */
    @Login
    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传非项目文件",httpMethod = "POST")
    public R file(@RequestParam("file") MultipartFile file,
                  RedirectAttributes red,
                  @LoginUser MemberUserEntity entity) throws IOException {
        String filePath = null;
        Long userId = entity.getUserId();
        if(file.isEmpty()){
            red.addAttribute("failMsg","文件选择不能为空");
            return R.error(EnumToKeyUtil.toInt(Code.REQUEST_PARAM_ERROR),"无效文件");
        }
        try {
            filePath = aliyunOSSUtil.upload(file.getInputStream(),
                    new File(file.getOriginalFilename()),
                    String.valueOf(userId));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().put("filePath",filePath);
    }
}
